package cao.oschina.widget.dialog;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import cao.oschina.R;


/**
 * 警告提示框
 * <p/>
 * Created by haidy on 14/12/23.
 */
public class AlertDialog extends BaseDialog {
    /**
     * Create a Dialog window that uses the default dialog frame style.
     *
     * @param context The Context the Dialog is to run it.  In particular, it
     *                uses the window manager and theme in this context to
     *                present its UI.
     */
    public AlertDialog(Context context) {
        this(context, R.style.dialog_style);
    }

    /**
     * Create a Dialog window that uses a custom dialog style.
     *
     * @param context The Context in which the Dialog should run. In particular, it
     *                uses the window manager and theme from this context to
     *                present its UI.
     * @param theme   A style resource describing the theme to use for the
     *                window. See <a href="{@docRoot}guide/topics/resources/available-resources.html#stylesandthemes">Style
     *                and Theme Resources</a> for more information about defining and using
     */
    public AlertDialog(Context context, int theme) {
        super(context, theme);
        init();
    }

    /**
     * 初始化
     */
    protected void init() {
        initView();
    }

    /**
     * 初始化view
     */
    private void initView() {
        setActionView(R.layout.dialog_action_view);
    }

    @Override
    public void setTitle(CharSequence title) {
        ((TextView) getTitleView().findViewById(R.id.title)).setText(title);
    }



    /**
     * 设置显示的文字内容
     *
     * @param stringID id
     */
    public void setContent(int stringID) {
        setContent(getContext().getString(stringID));
    }

    /**
     * 设置显示的文字内容
     *
     * @param content 内容
     */
    public AlertDialog setContent(String content) {
        ((TextView) findViewById(R.id.content)).setText(content);
        return this;
    }

    /**
     * 添加一个action
     *
     * @param actionName 名称
     * @param id         id
     * @return 添加的view
     */
    public AlertDialog addAction(String actionName, int id) {
        TextView action = (TextView) View.inflate(getContext(), R.layout.dialog_action, null);
        action.setText(actionName);
        action.setId(id);
        ((ViewGroup) getActionView()).addView(action);
        return this;
    }

    /**
     * 设置ActionView的点击事件
     *
     * @param listener 回调接口
     */
    public AlertDialog setOnActionListener(View.OnClickListener listener) {
        ViewGroup viewGroup = (ViewGroup) getActionView();
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            viewGroup.getChildAt(i).setOnClickListener(listener);
        }
        return this;
    }

    /**
     * 清除所有的Action
     */
    public void clearAction() {
        ViewGroup viewGroup = (ViewGroup) getActionView();
        viewGroup.removeAllViews();
    }
}
